
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

util.PrecacheModel("models/Items/HealthKit.mdl")
util.PrecacheModel("models/Items/BoxBuckshot.mdl")

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetMoveType( MOVETYPE_NONE )
	self.Entity:SetSolid( SOLID_NONE )
	self.Entity:DrawShadow( false )
	
	// Make the bbox short so we can jump over it
	// Note that we need a physics object to make it call triggers
	self.Entity:SetCollisionBounds( Vector( -32, -32, -32 ), Vector( 32, 32, 0 ) )
	self.Entity:PhysicsInitBox( Vector( -32, -32, -32 ), Vector( 32, 32, 0 ) )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableCollisions( false )		
	end
	
	self.Entity:SetCollisionGroup( COLLISION_GROUP_DEBRIS_TRIGGER )
	self.Entity:SetTrigger( true )
	self.Entity:SetNotSolid( true )
	
	self:SetActive( true )
	self.Amount = self.Amount or 25
	self.AmmoType = "Pistol"
	
	if (self:GetPickupItem() == "ammo") then			
		self.Entity:SetAngles( Angle(0,0,0) )
		self.Entity:SetModel("models/Items/BoxBuckshot.mdl")
	elseif (self:GetPickupItem() == "health") then		
		self.Entity:SetAngles( Angle(90,0,0) )
		self.Entity:SetModel("models/Items/HealthKit.mdl")
	else
		self.Entity:SetAngles( Angle(45,0,0) )
	end
end

function ENT:KeyValue( key, value )
	if ( key == "type" ) then				self:SetPickupItem( value )
	elseif ( key == "amount" ) then			self.Amount = tonumber(value) or 25
	elseif ( key == "ammotype" ) then		self.AmmoType = value						
	end
end

/*---------------------------------------------------------
   Name: Touch
---------------------------------------------------------*/
function ENT:Touch( entity )

	if (!self:GetActive()) then return end
	if (!entity:IsPlayer()) then return end
	
	self:SetActive( false )
	
	if (self:GetPickupItem() == "health") then
		entity:SetHealth( entity:Health() + self.Amount )
	elseif (self:GetPickupItem() == "ammo") then
		entity:GiveAmmo( self.Amount, self.AmmoType )
	else
		entity:Give( self:GetPickupItem() );
	end	
	
	local Pos = self.Entity:GetPos()
		
	if (self:GetPickupItem() == "health") then		Pos.z = Pos.z + 24
	elseif (self:GetPickupItem() == "ammo") then	Pos.z = Pos.z + 24
	end
	
	local effectdata = EffectData()
		effectdata:SetOrigin( Pos )
	util.Effect( "pickup", effectdata, true, true ) // Allow override, Ignore prediction
	
	self.Entity:Remove()	
end

